package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.PageResult7;
import com.itheima.common.Simple;
import com.itheima.entity.*;
import com.itheima.mapper.SubjectsDirectorysMapper;
import com.itheima.mapper.SubjectsMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.service.SubjectsDirectorysService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author cfr
 * @date 2022/1/10
 * @TIME:15:30
 */
@Service
public class SubjectsDirectorysServiceImpl extends ServiceImpl<SubjectsDirectorysMapper, SubjectsDirectorys> implements SubjectsDirectorysService {
    @Autowired
    private SubjectsDirectorysMapper subjectsDirectorysMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SubjectsMapper subjectsMapper;

    @Override
    public PageResult7 findPage(Long page, Long pagesize, String directoryName, Integer state, Integer subjectID) {
        //分页查询
        IPage<SubjectsDirectorys> p = new Page<>(page, pagesize);
        LambdaQueryWrapper<SubjectsDirectorys> wrapper = new LambdaQueryWrapper<>();
        if (directoryName != null) {
            wrapper.like(SubjectsDirectorys::getDirectoryName, directoryName);
        }
        if (null != state) {
            wrapper.eq(SubjectsDirectorys::getState, state);
        }
        if (null != subjectID) {
            wrapper.eq(SubjectsDirectorys::getSubjectID, subjectID);
        }
        IPage<SubjectsDirectorys> subjectsTagsIPage = subjectsDirectorysMapper.selectPage(p, wrapper);
        String s = JSON.toJSONString(subjectsTagsIPage.getRecords());

        List<SubjectsDirectorysDto> subjectsDirectorysDtos = JSON.parseArray(s, SubjectsDirectorysDto.class);
        for (SubjectsDirectorysDto subjectsDirectorysDto : subjectsDirectorysDtos) {
            subjectsDirectorysDto.setUsername(userMapper.selectById(subjectsDirectorysDto.getCreatorID()).getUsername());
            subjectsDirectorysDto.setSubjectName(subjectsMapper.selectById(subjectsDirectorysDto.getId()).getSubjectName());

        }
        //赛值
        PageResult7 pageResult7 = new PageResult7();
        pageResult7.setCounts(subjectsTagsIPage.getTotal());
        pageResult7.setPage(String.valueOf(page));
        pageResult7.setPagesize(String.valueOf(pagesize));
        pageResult7.setPages(p.getPages());
        pageResult7.setItems(subjectsDirectorysDtos);
        return pageResult7;
    }

    @Override
    public String add(SubjectsDirectorys subjectsDirectorys) {
        //补全数据添加
        subjectsDirectorys.setAddDate(new Date());
        subjectsDirectorys.setTotals(0);
        subjectsDirectorys.setState((short) 1);

        subjectsDirectorysMapper.insert(subjectsDirectorys);
        return "{\"success\": \" true\"}";
    }

    @Override
    public String state(Integer id, Integer state) {
        SubjectsDirectorys subjectsDirectorys = subjectsDirectorysMapper.selectById(id);
        subjectsDirectorys.setState(state.shortValue());

        //判断禁用更改学科二级目录数量
        Subjects subjects = subjectsMapper.selectById(subjectsDirectorys.getSubjectID());
        if (state == 0) {
            subjects.setTwoLevelDirectory(subjects.getTwoLevelDirectory() - 1);
        }
        if (state == 1) {
            subjects.setTwoLevelDirectory(subjects.getTwoLevelDirectory() + 1);
        }
        subjectsMapper.updateById(subjects);

        subjectsDirectorysMapper.updateById(subjectsDirectorys);
        return "{\"success\": \" true\"}";
    }

    @Override
    public String modify(SubjectsDirectorys subjectsDirectorys) {
        SubjectsDirectorys oldsubjectsDirectorys = subjectsDirectorysMapper.selectById(subjectsDirectorys.getId());
        oldsubjectsDirectorys.setSubjectID(subjectsDirectorys.getSubjectID());
        oldsubjectsDirectorys.setDirectoryName(subjectsDirectorys.getDirectoryName());
        //更改二级目录的数量
        //添加改学科目录数量
        Subjects newSubjects = subjectsMapper.selectById(oldsubjectsDirectorys.getSubjectID());
        newSubjects.setTwoLevelDirectory(newSubjects.getTwoLevelDirectory() + 1);

        subjectsMapper.updateById(newSubjects);

        subjectsDirectorysMapper.updateById(oldsubjectsDirectorys);
        return "{\"success\": \" true\"}";
    }

    @Override
    public String delete(SubjectsDirectorysDto subjectsDirectorysDto) {
        //更改学科二级目录数量
        Subjects subjects = subjectsMapper.selectById(subjectsDirectorysDto.getSubjectID());
        subjects.setTwoLevelDirectory(subjects.getTwoLevelDirectory() - 1);
        subjectsMapper.updateById(subjects);

        subjectsMapper.deleteById(subjectsDirectorysDto.getId());
        return "{\"success\": \" true\"}";
    }

    @Override
    public SubjectsDirectorysDto findById(Integer id) {
        SubjectsDirectorys subjectsDirectorys = subjectsDirectorysMapper.selectById(id);
        SubjectsDirectorysDto subjectsDirectorysDto = new SubjectsDirectorysDto();
        BeanUtils.copyProperties(subjectsDirectorys, subjectsDirectorysDto);
        subjectsDirectorysDto.setCreator(userMapper.selectById(subjectsDirectorysDto.getCreatorID()).getUsername());
        return subjectsDirectorysDto;
    }

    @Override
    public List<Simple> findDirectorys(Integer subjectID) {
        ArrayList<Simple> simples = new ArrayList<>();
        LambdaQueryWrapper<SubjectsDirectorys> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SubjectsDirectorys::getSubjectID, subjectID);
        List<SubjectsDirectorys> subjectsDirectorys = subjectsDirectorysMapper.selectList(wrapper);
        for (SubjectsDirectorys subjectsDirectory : subjectsDirectorys) {
            Simple simple = new Simple();
            simple.setLabel(subjectsDirectory.getDirectoryName());
            simple.setValue(subjectsDirectory.getId());
            simples.add(simple);
        }
        return simples;
    }
}
